package com.laviedesfons.jhmtasc.metadata;

import com.laviedesfons.jhmtasc.types.abstracts.AbstractType;
import com.laviedesfons.jhmtasc.types.abstracts.Interface;
import com.laviedesfons.jhmtasc.types.interfaces.InternalType;
import java.util.HashMap;
import java.util.ServiceLoader;

/**
 *
 * @author Romano
 */
public abstract class TypeUtils {

    public static TypeUtils getTypeUtils(){
        return Holder.INSTANCE;
    }

    private static class Holder {
            public static final TypeUtils INSTANCE = load();

            static TypeUtils load(){
                ServiceLoader<TypeUtils> serviceLoader = ServiceLoader.load(TypeUtils.class);
                return serviceLoader.iterator().next();
            }
    }

    final protected HashMap<Class<? extends AbstractType>,MetaModel> metaTypes = new HashMap<Class<? extends AbstractType>,MetaModel>();

    protected abstract Class<? extends AbstractType> getRootTypeClass();

    protected void addMetaModel(MetaModel mt){
        metaTypes.put(mt.getClazz(), mt);
    }

    public MetaModel getMetaModel(Class<? extends AbstractType> s){
        return metaTypes.get(s);
    }

    public Interface createRootType() {
        MetaModel mt = getMetaModel(getRootTypeClass());
        if(mt != null)
            return (Interface) mt.create();
        return null;
    }

    public boolean isRootType(Class<? extends InternalType> aClass){
        return aClass.equals(getRootTypeClass());
    }
}
